package com.harun;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Scanner;

public class MainApp {

	public static final String ANSI_RESET = "\u001B[0m";
	public static final String ANSI_BLACK = "\u001B[30m";
	public static final String ANSI_RED = "\u001B[31m";
	public static final String ANSI_GREEN = "\u001B[32m";
	public static final String ANSI_YELLOW = "\u001B[33m";
	public static final String ANSI_BLUE = "\u001B[34m";
	public static final String ANSI_PURPLE = "\u001B[35m";
	public static final String ANSI_CYAN = "\u001B[36m";
	public static final String ANSI_WHITE = "\u001B[37m";
	
	private List<Word> wordsList = new ArrayList<Word>();
	
	public MainApp() {

		String fileName = "gre-words1.csv";
		loadWordsFromFile(fileName);
		
		try {
			
//			offerRandomWordByWord(1);
			
			offerRandomWordByMeaning(1);
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		 
	}
	
	private void loadWordsFromFile(String fileName) {
		
		File file = new File(fileName);
		
		BufferedReader reader = null;
		try {
			InputStreamReader fileReader = new InputStreamReader(new FileInputStream(file), "UTF8");
			reader = new BufferedReader(fileReader);
			System.out.println("Reading from file [" + fileName + "] ");
			String line = "";
			int lineCount=1;
			while((line = reader.readLine()) != null) {
				
				try {
				
					String items[] = line.split(";");
					int value = Integer.parseInt(items[0]);
					
					Word word = new Word(items[1], items[2]);
					word.setValue(value);
					
					wordsList.add(word);
					
				} catch(Exception e) { // not important then skip
					System.out.println(ANSI_PURPLE + "Line " + lineCount + ". Hata : " + e.getMessage() + ANSI_RESET);
				}
				
				lineCount++;
			}
			System.out.println("Totally " + lineCount + " line read.");
			System.out.println("Totally " + wordsList.size() + " word(s) found.");
			
			System.out.println("-------------------Program Started-------------------");
			
			
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (NumberFormatException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				if(reader != null)
				reader.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
	
	public void offerRandomWordByWord(int val) throws IOException {
		Scanner scanner = new Scanner(System.in);
		Collections.shuffle(wordsList);
		int count = 1;
		for (Word word : wordsList) {
			
			if(word.getValue() != val) continue;
			
			System.out.print(ANSI_RED + count++ + ". " + word.getWord() + " : ");
			scanner.nextLine();
			System.out.println(ANSI_BLACK + word.getMeaning() + "\n");
		}
		scanner.close();
	}
	
	public void offerRandomWordByMeaning(int val) throws IOException {
		Scanner scanner = new Scanner(System.in);
		Collections.shuffle(wordsList);
		int count = 1;
		for (Word word : wordsList) {
			
			if(word.getValue() != val) continue;
			
			System.out.print(ANSI_BLACK + count++ + ". " + word.getMeaning() + " : ");
			scanner.nextLine();
			System.out.println(ANSI_RED + word.getWord() + "\n");
		}
		scanner.close();
	}
	
	public static void main(String[] args) {
		System.out.print(ANSI_RESET);
		new MainApp();
		
	}

}
